package org.example.servicelisten.listen;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.model.manage.bean.po.Student;
import org.example.model.student.bean.Task;
import org.example.servicelisten.service.StudentService;
import org.example.servicelisten.websocket.WebSocketServer;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class TaskListen {

    @Autowired
    private StudentService studentService;
    
    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(name = "task.add.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void receive_add(Task task) {
        Set<String> sids = studentService.getAllByMajorId(task.getMajorId()).stream().map(Student::getUnionId).collect(Collectors.toSet());
        
        try {
            String taskJson = objectMapper.writeValueAsString(task);
            WebSocketServer.sendToStudents(new String[]{"add", taskJson}, sids);
        } catch (Exception e) {
            log.error("序列化Task对象失败", e);
            // 备用方案，使用简单的字符串格式
            WebSocketServer.sendToStudents(new String[]{"add", task.toString()}, sids);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(name = "task.delete.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void receive_delete(Integer[] ids) {
        Set<String> sids = studentService.getAllByMajorId(ids[0]).stream().map(Student::getUnionId).collect(Collectors.toSet());
        
        try {
            String idsJson = objectMapper.writeValueAsString(ids);
            WebSocketServer.sendToStudents(new String[]{"delete", idsJson}, sids);
        } catch (Exception e) {
            log.error("序列化删除ID数组失败", e);
            WebSocketServer.sendToStudents(new String[]{"delete", java.util.Arrays.toString(ids)}, sids);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(),
            exchange = @Exchange(name = "task.update.fanout", type = ExchangeTypes.FANOUT)
    ))
    public void receive_update(Task task) {
        Set<String> sids = studentService.getAllByMajorId(task.getMajorId()).stream().map(Student::getUnionId).collect(Collectors.toSet());
        
        try {
            String taskJson = objectMapper.writeValueAsString(task);
            WebSocketServer.sendToStudents(new String[]{"update", taskJson}, sids);
        } catch (Exception e) {
            log.error("序列化Task对象失败", e);
            WebSocketServer.sendToStudents(new String[]{"update", task.toString()}, sids);
        }
    }

}
